let publicFun = {
  install(Vue) {
    Vue.prototype.publicFun = {
      rebuildTime: function (time, type) {
        if (!time) return '-'
        const now = new Date().getTime()
        const date = new Date(time),
          Y = date.getFullYear(),
          M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1),
          D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()),
          h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()),
          m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
        // s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        const dateTimestamp = date.getTime()
        if (type === 1) {
          if (now - dateTimestamp < 24 * 3600 * 1000) {
            return '今天 ' + h + ':' + m
          } else if (now - dateTimestamp < 48 * 3600 * 1000) {
            return '昨天 ' + h + ':' + m
          } else {
            return String(Y).concat('-', M, '-', D, ' ', h, ':', m)
          }
        } else if (type === 2) {
          return String(Y).concat('/', M, '/', D, ' ', h, ':', m)
        }else if (type === 3){
          return String(Y).concat('-', M, '-', D, ' ', h, ':', m)
        }
        return String(Y).concat(M, D)
      },
      getFileSize: function (size, fixed) {
        if (!size || size === -1) {
          return '-'
        }
        const currentFixed = fixed || 2
        if (size < 1024) {
          return size + 'B'
        } else if (size < 1048576) {   //1024 * 1024
          return (size / 1024).toFixed(currentFixed) + 'K'
        } else if (size < 1073741824) {   //1024 * 1024 * 1024
          return (size / 1048576).toFixed(currentFixed) + 'M'
        } else if (size < 1099511627776) {  //1024 * 1024 * 1024 * 1024
          return (size / 1073741824).toFixed(currentFixed) + 'G'
        } else if (size < 1125899906842624) {   //1024 * 1024 * 1024 * 1024 * 1024
          return (size / 1099511627776).toFixed(currentFixed) + 'T'
        }
      },
      characterSort: function (array) { //汉字排序
        const newArr = array.sort(function (val1, val2) {
            return val1.name.localeCompare(val2.name, 'zh')
          }
        );
        return this.pySegSort(newArr)
      },
      pySegSort(arr) {
        if (!String.prototype.localeCompare)
          return null;
        let letters = "*abcdefghjklmnopqrstwxyz".split('');
        let zh = "阿八嚓哒妸发旮哈讥咔垃痳拏噢妑七呥扨它穵夕丫帀".split('');

        let segs = [];
        let curr;
        letters.forEach(function (item, i) {
          curr = {letter: item.toUpperCase(), data: []};
          arr.forEach(function (item2) {
            if ((!zh[i - 1] || zh[i - 1].localeCompare(item2.name) <= 0) && item2.name.localeCompare(zh[i]) === -1) {
              curr.data.push(item2);
            }
          });
          if (curr.data.length) {
            curr.data.sort(function (a, b) {
              return a.name.localeCompare(b.name);
            });
            segs.push(curr);
          }
        });
        return segs;
      },

      deepClone(data) {
        let type = typeof data;
        let obj;
        if (type === 'array') {
          obj = [];
        } else if (type === 'object') {
          obj = {};
        } else {
          //不再具有下一层次
          return data;
        }
        if (type === 'array') {
          for (let i = 0, len = data.length; i < len; i++) {
            obj.push(this.deepClone(data[i]));
          }
        } else if (type === 'object') {
          for (let key in data) {
            obj[key] = this.deepClone(data[key]);
          }
        }
        return obj;
      },
      getOs: function () {
        let ua = navigator.userAgent,
          isWindowsPhone = /(?:Windows Phone)/.test(ua),
          isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
          isAndroid = /(?:Android)/.test(ua),
          isFireFox = /(?:Firefox)/.test(ua),
          // isChrome = /(?:Chrome|CriOS)/.test(ua),
          isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
          isiPhone = /(?:iPhone)/.test(ua) && !isTablet,
          isPc = !isPhone && !isAndroid && !isSymbian,
          isPhone = isiPhone || isAndroid
        return {
          isPhone: isPhone,
          isTablet: isTablet,
          isiPhone: isiPhone,
          isAndroid: isAndroid,
          isPc: isPc
        };
      },
      checkIcon: function (type) {
        let iconClass = ''
        switch (type) {
          case 'js':
            iconClass = 'icon-3-2';
            break;
          case 'txt':
          case 'aspx':
          case 'config':
          case 'cs':
          case 'master':
          case 'asax':
          case 'php':
          case 'jsp':
          case 'asp':
          case 'as':
          case 'rtf':
          case 'h':
          case 'cpp':
            iconClass = 'icon-5-2';
            break;
          case 'html':
          case 'htm':
          case 'xml':
            iconClass = 'icon-5-1';
            break;
          case 'ai':
            iconClass = 'icon-3-1';
            break;
          case 'psd':
            iconClass = 'icon-2-1';
            break;
          case 'png':
          case 'jpg':
          case 'jpge':
          case 'jpeg':
          case 'bmp':
          case 'jif':
          case 'ico':
          case 'tiff':
          case 'eps':
          case 'tif':
          case 'msp':
            iconClass = 'icon-4-2';
            break;
          case 'gif':
            iconClass = 'icon-4-3';
            break;
          case 'asf':
          case 'avi':
          case 'mov':
          case 'rm':
          case 'mp2v':
          case 'mkv':
          case 'mp4':
          case 'mpeg':
          case 'wmv':
          case 'mmm':
          case 'dvd':
          case 'vob':
          case 'mpg':
          case 'rmvb':
            iconClass = 'icon-5-3';
            break;
          case 'ac3':
          case 'aiff':
          case 'au':
          case 'mp3':
          case 'm4a':
          case 'mp2':
          case 'ra':
          case 'wav':
          case 'wpl':
          case 'wma':
            iconClass = 'icon-6-2';
            break;
          case 'doc':
          case 'docx':
            iconClass = 'icon-2-2';
            break;
          case 'ppt':
          case 'pptx':
            iconClass = 'icon-6-4';
            break;
          case 'xls':
          case 'xlsx':
            iconClass = 'icon-4-1';
            break;
          case 'pdf':
            iconClass = 'pdf';
            break;
          case 'rar':
          case '7z':
          case 'zip':
          case 'iso':
            iconClass = 'icon-7-2';
            break;
          case 'dwf':
            iconClass = 'icon-3-4';
            break;
          case 'rp':
            iconClass = 'icon-3-3';
            break;
          case 'rvt':
            iconClass = 'icon-6-1';
            break;
          case 'skp':
            iconClass = 'icon-7-4';
            break;
          case 'stl':
            iconClass = 'icon-6-3';
            break;
          case 'swf':
          case 'fle':
            iconClass = 'icon-7-1';
            break;
          case 'vsd':
            iconClass = 'icon-2-3';
            break;
          case 'xmind':
            iconClass = 'icon-7-3';
            break;
          default:
            iconClass = 'icon-1-4';
            break;
        }
        return iconClass
      }

    }
  }
}

export default publicFun
